stages:
  - analysis
  - test

.lab_custom_check:
  image: registry.gitlab.com/iu5edu/docker/uchardet:latest
  tags:
    - iu5edu
  rules:
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH
      changes:
        - ${LAB_DIR}/**/*
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual
  variables:
    SRC_DIRECTORY: "${CI_PROJECT_DIR}/${LAB_DIR}"
    TESTS_DIR_NAME: tests_${CI_JOB_ID}
    TESTS_DIR: ${CI_BUILDS_DIR}/${TESTS_DIR_NAME}
  before_script:
    - rm -rf ${TESTS_DIR}
    - cd ${CI_BUILDS_DIR}
    - git clone https://hub.mos.ru/iu5bmstu/lections-and-labs/cpp1/tests.git ${TESTS_DIR_NAME}
  script:
    - cd ${TESTS_DIR}
    - bash encoding_check.sh
  after_script:
    - rm -rf ${TESTS_DIR}

.lab_test:
  image: registry.gitlab.com/iu5edu/docker/cpp-pytest:latest
  tags:
    - iu5edu
  rules:
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH
      changes:
        - ${LAB_DIR}/**/*
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      when: manual
  variables:
    WORK_DIRECTORY: ${CI_BUILDS_DIR}/build_${CI_JOB_ID}
    PROGRAM_UNDER_TEST: "./${LAB_DIR}"
    TESTS_DIR_NAME: tests_${CI_JOB_ID}
    TESTS_DIR: ${CI_BUILDS_DIR}/${TESTS_DIR_NAME}
  before_script:
    - rm -rf ${TESTS_DIR}
    - rm -rf ${BUILD_DIR} 
    - cd ${CI_BUILDS_DIR}
    - git clone https://hub.mos.ru/iu5bmstu/lections-and-labs/cpp1/tests.git ${TESTS_DIR_NAME}
  script:
    - mkdir -p ${WORK_DIRECTORY}
    - cd ${CI_PROJECT_DIR}/${LAB_DIR}
    - cmake -S ./ -B ${WORK_DIRECTORY}
    - cmake --build ${WORK_DIRECTORY}
    - cd ${TESTS_DIR}/${LAB_DIR}
    - pytest tests.py --junitxml=${CI_PROJECT_DIR}/rspec.xml -s
  after_script:
    - rm -rf ${TESTS_DIR}
    - rm -rf ${BUILD_DIR} 
  artifacts:
    when: always
    paths:
      - rspec.xml
    reports:
      junit: rspec.xml

# lab1
lab1_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab1"
  extends: .lab_custom_check

lab1_test:
  stage: test
  variables:
    LAB_DIR: "lab1"
  extends: .lab_test
  needs: ["lab1_custom_check"]

# lab2
lab2_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab2"
  extends: .lab_custom_check

lab2_test:
  stage: test
  variables:
    LAB_DIR: "lab2"
  extends: .lab_test
  needs: ["lab2_custom_check"]

# lab3_1
lab3_1_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab3_1"
  extends: .lab_custom_check

lab3_1_test:
  stage: test
  variables:
    LAB_DIR: "lab3_1"
  extends: .lab_test
  needs: ["lab3_1_custom_check"]

# lab3_2
lab3_2_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab3_2"
  extends: .lab_custom_check

lab3_2_test:
  stage: test
  variables:
    LAB_DIR: "lab3_2"
  extends: .lab_test
  needs: ["lab3_2_custom_check"]

# lab3_3
lab3_3_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab3_3"
  extends: .lab_custom_check

lab3_3_test:
  stage: test
  variables:
    LAB_DIR: "lab3_3"
  extends: .lab_test
  needs: ["lab3_3_custom_check"]

# lab3_4
lab3_4_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab3_4"
  extends: .lab_custom_check

lab3_4_test:
  stage: test
  variables:
    LAB_DIR: "lab3_4"
  extends: .lab_test
  needs: ["lab3_4_custom_check"]

# lab4
lab4_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab4"
  extends: .lab_custom_check

lab4_test:
  stage: test
  variables:
    LAB_DIR: "lab4"
  extends: .lab_test
  needs: ["lab4_custom_check"]

# lab5
lab5_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab5"
  extends: .lab_custom_check

lab5_test:
  stage: test
  variables:
    LAB_DIR: "lab5"
  extends: .lab_test
  needs: ["lab5_custom_check"]

# lab6
lab6_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab6"
  extends: .lab_custom_check

lab6_test:
  stage: test
  variables:
    LAB_DIR: "lab6"
  extends: .lab_test
  needs: ["lab6_custom_check"]

# lab7
lab7_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab7"
  extends: .lab_custom_check

lab7_test:
  stage: test
  variables:
    LAB_DIR: "lab7"
  extends: .lab_test
  needs: ["lab7_custom_check"]

# lab8
lab8_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab8"
  extends: .lab_custom_check

lab8_test:
  stage: test
  variables:
    LAB_DIR: "lab8"
  extends: .lab_test
  needs: ["lab8_custom_check"]

# lab9
lab9_custom_check:
  stage: analysis
  variables:
    LAB_DIR: "lab9"
  extends: .lab_custom_check

lab9_test:
  stage: test
  variables:
    LAB_DIR: "lab9"
  extends: .lab_test
  needs: ["lab9_custom_check"]
